import { RequestUtil } from '@/util/ele_server_req';
function isDate(obj) {
  return obj instanceof Date || Object.prototype.toString.call(obj) === '[object Date]';
}
const splitDate = (dateTimeString: any) => {
	if(isDate(dateTimeString)){
		return toStandardDateStr(dateTimeString);
	}else{
		const date = new Date(dateTimeString);
	return toStandardDateStr(date);
	}
};
const toStandardDateStr = (date: Date) => {
	let d = {
		YYYY: date.getFullYear(),
		MM: String(date.getMonth() + 1).padStart(2, '0'),
		DD: String(date.getDate()).padStart(2, '0'),
		hh: String(date.getHours()).padStart(2, '0'),
		mm: String(date.getMinutes()).padStart(2, '0'),
		ss: String(date.getSeconds()).padStart(2, '0')
	};
	return d;
};
export const DateFormatUtil = {
	//将日期拆分
	getDateObj: (dateTimeString: any) => {
		return splitDate(dateTimeString);
	},
	formatDateYMD: (dateTimeString: any) => {
		let d = splitDate(dateTimeString);
		return d.YYYY + '-' + d.MM + '-' + d.DD;
	},
	formatDateYMD_zh: (dateTimeString: any) => {
		let d = splitDate(dateTimeString);
		return d.YYYY + '年' + d.MM + '月' + d.DD + '日';
	},
	formatDateYM_zh: (dateTimeString: any) => {
		let d = splitDate(dateTimeString);
		return d.YYYY + '年' + d.MM + '月';
	},
	formatDateYMDHMS: (dateTimeString: any) => {
		let d = splitDate(dateTimeString);
		return d.YYYY + '-' + d.MM + '-' + d.DD + ' ' + d.hh + ':' + d.mm + ':' + d.ss;
	},
	formatDateYMDHMS_zh: (dateTimeString: any) => {
		let d = splitDate(dateTimeString);
		return d.YYYY + '年' + d.MM + '月' + d.DD + '日' + ' ' + d.hh + ':' + d.mm + ':' + d.ss;
	},
	formatDateYMDHM: (dateTimeString: any) => {
		let d = splitDate(dateTimeString);
		return d.YYYY + '-' + d.MM + '-' + d.DD + ' ' + d.hh + ':' + d.mm;
	}
};
//获取服务器时间
export const GetServerTime = async () => {
	let now = '';
	await RequestUtil.do_get_async(
		'/common/getServerTime',
		{},
		response => {
			if (response.data.code === 200) {
				now = response.data.data;
				if (now == undefined) {
					now = '';
				}
			}
		},
		error => {
			console.log(error);
		}
	);
	return new Date(now);
};
// 将时间差转换为其他单位
const seconds = 1000;
const minutes = seconds * 60;
const hours = minutes * 60;
const days = hours * 24;
const years = days * 365;
export const TimeDifferenceUtil = {
	timeDifference2Minutes: (beforeTime: number, afterTime: number) => {
		const difference = afterTime - beforeTime; // 时间差（单位：毫秒）
		const absDifference = Math.abs(difference); // 绝对值
		if (absDifference < minutes) {
			return 0;
		}
		Math.round(absDifference / minutes);
	},
	//用中文表述时间差
	timeDifferenceLabel: (difference: number) => {
		const absDifference = Math.abs(difference); // 绝对值
		let label = '';
		// 返回值
		if (absDifference < seconds * 2) {
			return difference > 0 ? '刚刚' : '马上';
		} else if (absDifference < minutes) {
			label = Math.round(absDifference / seconds) + '秒';
		} else if (absDifference < hours) {
			label = Math.round(absDifference / minutes) + '分钟';
		} else if (absDifference < days) {
			label = Math.round(absDifference / hours) + '小时';
		} else if (absDifference < years) {
			label = Math.round(absDifference / days) + '天';
		} else {
			const _year_num = Math.round(absDifference / years);
			if (_year_num <= 10) {
				label = _year_num + '年';
			} else {
				return difference > 0 ? '很久以前' : '很久以后';
			}
		}
		return difference > 0 ? label + '前' : label + '后';
	},
	getDaysBetweenDates: (date1, date2) => {
		// 将日期字符串转换为Date对象
		let d1 = new Date(date1);
		let d2 = new Date(date2);
		// 计算两个日期的时间差（毫秒）
		let timeDiff = d2.getTime() - d1.getTime();
		// 计算天数
		let days = Math.ceil(timeDiff / (1000 * 3600 * 24));
		return days;
	}
};
